package com.bjqh.edu.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.bjqh.edu.UI.Activity.StudentPersonal.OrderActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
    public static String orderNum;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        switch (req.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
//                goToGetMsg();
                Log.i("ll", " ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX");
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
//                goToShowMsg((ShowMessageFromWX.Req) req);
                Log.i("ll", ConstantsAPI.COMMAND_LAUNCH_BY_WX+"ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX");
                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
//                Toast.makeText(this, R.string.launch_from_wx, Toast.LENGTH_SHORT).show();
                Log.i("ll", ConstantsAPI.COMMAND_LAUNCH_BY_WX+"");
                break;

            default:
                break;
        }
    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {

//        if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
//            Toast.makeText(this, "code = " + ((SendAuth.Resp) resp).code, Toast.LENGTH_SHORT).show();
//        }

        int result = 0;
        Log.i("ll",resp.openId+"");
        Log.i("ll",resp.errCode+"");
        Log.i("ll",resp.errStr+"");
        Log.i("ll",resp.transaction+"");

        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
//				支付成功
//                result = R.string.errcode_success;
              //  Log.i("lsy", orderNum);
                Toast.makeText(this,"支付成功",Toast.LENGTH_LONG).show();
                setResult(1);
                startActivity(new Intent(WXPayEntryActivity.this,OrderActivity.class));
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
//				用户取消支付
                setResult(1);
                finish();
                Log.i("lsy", "ERR_USER_CANCEL");
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
//				授权绑定失败
                setResult(1);
                finish();
                Log.i("lsy", "授权绑定失败");
                break;
            case BaseResp.ErrCode.ERR_SENT_FAILED:
//				发送请求失败
//                result = R.string.errcode_deny;
                Log.i("lsy", "ERR_SENT_FAILED");
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
//				微信版本不支持
                finish();
                Log.i("lsy", "微信版本不支持");

                break;
            case BaseResp.ErrCode.ERR_COMM:
//			签名错误
                setResult(0);
                finish();
                Log.i("lsy", "\t签名错误");
                break;
            default:
//                result = R.string.errcode_unknown;
                finish();
                Log.i("lsy", "未知错误");
                break;
        }

    }

}